local yanhuo = fk.CreateSkill{
  name = "xiaobai__yanhuo",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["xiaobai__yanhuo"] = "言祸",
  [":xiaobai__yanhuo"] = "转换技，阳：准备阶段，你可以进行一次【闪电】判定;"..
    "阴：其他角色的结束阶段，其可以令你执行阳项，若你因此进入濒死状态，删去“言祸”转换技标签。",

  
  [":xiaobai__yanhuo_yang"] = "转换技，<font color=\"#E0DB2F\">阳：准备阶段，你可以进行一次【闪电】判定；</font>"..
    "阴：其他角色的结束阶段，其可以令你执行阳项，若你因此进入濒死状态，删去“言祸”转换技标签。",
  [":xiaobai__yanhuo_yin"] = "转换技，阳：准备阶段，你可以进行一次【闪电】判定；"..
    "<font color=\"#E0DB2F\">阴：其他角色的结束阶段，其可以令你执行阳项，若你因此进入濒死状态，删去“言祸”转换技标签。</font>",

  ["#xiaobai__yanhuo-yang"] = "言祸：是否进行一次【闪电】判定？",
  ["#xiaobai__yanhuo-yin"] = "言祸：是否令 %src 进行一次【闪电】判定？",
  ["#xiaobai__yanhuo-use"] = "言祸：你可以使用其中至多两张牌",

  ["$xiaobai__yanhuo1"] = "岁在庚子，天下易代，刘氏祚尽矣。",
  ["$xiaobai__yanhuo2"] = "得蜀不和，必有所失，算在寅卯之间。",
}

yanhuo:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if target == player and player.phase == Player.Start then
        return player:getSwitchSkillState(yanhuo.name, false) == fk.SwitchYang
      elseif target ~= player and target.phase == Player.Finish then
        return not target.dead and player:getSwitchSkillState(yanhuo.name, false) == fk.SwitchYin
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if target == player then
      return player.room:askToSkillInvoke(player, {skill_name = yanhuo.name, prompt = "#xiaobai__yanhuo-yang"})
    else
      return player.room:askToSkillInvoke(target, {skill_name = yanhuo.name, prompt =  "#xiaobai__yanhuo-yin:"..player.id})
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target ~= player then
      room:doIndicate(target.id, {player.id})
    end
    local judge = {   ---@type JudgeDataSpec
      who = player,
      reason = "lightning",
      pattern = ".|2~9|spade",
    }
    room:judge(judge)
    if judge.card:matchPattern(judge.pattern) then
      local damage = {
        to = player,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = "lightning_skill",
      }
      if target ~= player then
        damage.extra_data = {xiaobai__yanhuo = player} 
      end
      room:damage(damage)
    end
    if player.dead then return end
    if player:getMark("xiaobai__yanhuo-tmp") > 0 then
      room:setPlayerMark(player, "xiaobai__yanhuo-tmp", 0)
      room:handleAddLoseSkills(player, "-xiaobai__yanhuo|xiaobai__yanhuo_without_switch")
    end
  end,
})

yanhuo:addEffect(fk.EnterDying, {
  can_refresh = function(self, event, target, player, data)
    return data.damage and data.damage.extra_data and data.damage.extra_data.xiaobai__yanhuo == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "xiaobai__yanhuo-tmp", 1)
  end
})

local yanhuo_without_switch = fk.CreateSkill{
  name = "xiaobai__yanhuo_without_switch",
}

yanhuo_without_switch:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.phase == Player.Start (player == target or not target.dead)
  end,
  on_cost = function(self, event, target, player, data)
    if target == player then
      return player.room:askToSkillInvoke(player, {skill_name = yanhuo.name, prompt = "#xiaobai__yanhuo-yang"})
    else
      return player.room:askToSkillInvoke(target, {skill_name = yanhuo.name, prompt =  "#xiaobai__yanhuo-yin:"..player.id})
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if target ~= player then
      room:doIndicate(target.id, {player.id})
    end
    local judge = {   ---@type JudgeDataSpec
      who = player,
      reason = "lightning",
      pattern = ".|2~9|spade",
    }
    room:judge(judge)
    if judge.card:matchPattern(judge.pattern) then
      local damage = {
        to = player,
        damage = 3,
        damageType = fk.ThunderDamage,
        skillName = "lightning_skill",
      }
      if target ~= player then
        damage.extra_data = {xiaobai__yanhuo = player} 
      end
      room:damage(damage)
    end
    if player.dead then return end
  end,
})

Fk:loadTranslationTable{
  ["xiaobai__yanhuo_without_switch"] = "言祸",
  [":xiaobai__yanhuo_without_switch"] = "准备阶段，你可以进行一次【闪电】判定；其他角色的结束阶段，其可以令你进行一次【闪电】判定",
}

return {yanhuo, yanhuo_without_switch}